package com.gosophia.reportData.action;

import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.gosophia.flowForm.entity.FlowForm;
import com.gosophia.flowForm.service.FlowFormManageService;
import com.gosophia.flowFormManagement.dto.FormInstanceDTO;
import com.gosophia.flowFormManagement.ws.FormInstanceWebService;
import com.gosophia.reportData.ws.ReportJobWebService;
import com.gosophia.util.FreemarkerManage;

import freemarker.template.Configuration;
import freemarker.template.Template;

@Component
public class DynaFormFunction {
	private static String defaultTemplate = "document_form.ftl";
	private static String showTemplate = "showform.ftl";
	private static String initForm = "initForm.ftl";
	private static FlowFormManageService flowFormManageService;
	private static FormInstanceWebService formInstanceWebService;
	private static ReportJobWebService reportJobWebService;

	/***
	 * 初始化表单信息
	 * @param workflowId
	 * @return
	 */
	public static String form(long workflowId) {
		FlowForm flowForm = flowFormManageService.findFlowFormById(workflowId);
		if (null == flowForm) {
			return null;
		}
		/**
		 * 加载freemarker的配置
		 */
		Configuration configuration = FreemarkerManage.getConfiguration();
		try {
			/**
			 * 加载表单模板文件
			 */
			Template template = configuration.getTemplate(initForm);
			Writer writer = new StringWriter();
			Map root = new HashMap();
			root.put("form", flowForm);
			/**
			 * 将模板与数据进行相结合
			 */
			template.process(root, writer);
			return writer.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 显示值
	 * @param formInstanceId
	 * @return
	 */
	public static String showForm(long formInstanceId) {

		try {
			FormInstanceDTO formInstanceDTO = formInstanceWebService
					.findFormInstanceById(formInstanceId);
			if (null == formInstanceDTO) {
				return null;
			}
			/**
			 * 加载freemarker的配置
			 */
			Configuration configuration = FreemarkerManage.getConfiguration();
			/**
			 * 获取动态表单的实例数据
			 */
			Map<Long, Object> dataInstance = formInstanceDTO.getInstanceData();
			Map<String, Object> newDataInstance = reportJobWebService
					.findFlowFormData(dataInstance);
			/**
			 * 根据配置加载模板
			 */
			Template template = configuration.getTemplate(showTemplate);
			Writer writer = new StringWriter();
			Map root = new HashMap();
			root.put("showForm", newDataInstance);
			/**
			 * 将模板与数据进行结合
			 */
			template.process(root, writer);
			return writer.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 初始化表单信息，并且初始化表单值
	 * @param formInstanceId
	 * @param workflowId
	 * @return
	 */
	public static String formInit(long workflowId,long formInstanceId) {
		FlowForm flowForm = flowFormManageService.findFlowFormById(workflowId);
		if (null == flowForm) {
			return null;
		}
		Configuration configuration = FreemarkerManage.getConfiguration();
		try {

			FormInstanceDTO formInstanceDTO = formInstanceWebService
			.findFormInstanceById(formInstanceId);
			Map<Long, Object> dataInstance = formInstanceDTO.getInstanceData();
			Template template = configuration.getTemplate(initForm);
			Writer writer = new StringWriter();
			Map root = new HashMap();
			Map<Object,Object> newInitForm=new HashMap<Object, Object>();
			for(Long key:dataInstance.keySet()){
				newInitForm.put(key.toString(), dataInstance.get(key));
			}
			root.put("form", flowForm);
			root.put("newInitForm", newInitForm);
			template.process(root, writer);
			return writer.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
		
	}

	@Autowired
	public void setFlowFormManageService(
			FlowFormManageService flowFormManageService) {
		DynaFormFunction.flowFormManageService = flowFormManageService;
	}

	@Autowired
	public void setFormInstanceWebService(
			FormInstanceWebService formInstanceWebService) {
		DynaFormFunction.formInstanceWebService = formInstanceWebService;
	}

	@Autowired
	public void setReportJobWebService(
			ReportJobWebService reportJobWebService) {
		DynaFormFunction.reportJobWebService = reportJobWebService;
	}

}